package com.cskaoyan.datasource;

import com.cskaoyan.datasource.opensource.C3p0Utils;
import com.cskaoyan.datasource.opensource.DBCPUtils;
import com.cskaoyan.datasource.opensource.DruidUtils;
import com.cskaoyan.jdbc.util.JDBCUtils;
import com.mysql.jdbc.JDBC4Connection;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 创建日期: 2022/06/05 11:13
 *
 * @author ciggar
 */
public class DatasourceDemo {

    public static void main(String[] args) throws SQLException {

//        DataSource dataSource = new MyConnectionPoolV4();

        // 获取连接
        Connection connection = DruidUtils.getConnection();

        System.out.println(connection.getClass());
//        class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
//        class com.mchange.v2.c3p0.impl.NewProxyConnection
//        class com.alibaba.druid.pool.DruidPooledConnection
        // 这些实例对象中的 close方法都是被改过的，其实都不是关闭连接，都是返回连接到数据库连接池



        // 获取statement
        Statement statement = connection.createStatement();

        // 执行SQL语句
        ResultSet resultSet = statement.executeQuery("select * from cskaoyan");

        // 解析结果集
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String teacherName = resultSet.getString("teacher_name");
            String nickname = resultSet.getString("nickname");

            System.out.println("id:" + id + ", teacherName:" +teacherName + ",nickname:" +nickname);
        }

        // 释放资源
        JDBCUtils.close(resultSet,statement,connection);
//        connection.close();
//        JDBC4Connection jdbc4Connection = new JDBC4Connection();
//        jdbc4Connection.close();

        // 返回connection
//        MyConnectionPoolV4.returnConnection(connection);


    }
}
